#include <string>

#include "Client.h"
#include "NetworkWrapper.h"
#include "EventWrapper.h"
#include "Configuration.h"

Client::Client(bool onServer)
        : MessageReceiver(NULL), onServer(onServer)
{
}

Client::~Client()
{
}

void Client::init()
{
    std::string ip =
            onServer ? Configuration::get(Configuration::LOCAL_HOST) : Configuration::get(Configuration::SERVER_IP);
    socket = NetworkWrapper::openClientSocket(ip);
}

void Client::send(NetworkEvent& event)
{
    void* data = event.getMessageBlob();
    size_t size = event.getMessageBlobSize();
    NetworkWrapper::sendTo(socket, data, size);
}

void Client::pushEvent(NetworkEvent::Header& header, void* data)
{
    int serverId = -1;
    NetworkEvent::Header* h = NetworkEvent::allocateHeader(header.code, serverId);
    EventWrapper::push(h, data);
}

void* Client::createDisconnectEvent(NetworkEvent::Header& header)
{
    header.code = NetworkEvent::CLIENT_DISCONNECTED;
    header.sizeOfData = 0;
    return NULL;
}

